package com.ycu.jk16.mall.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ycu.jk16.mall.domain.Product;
import com.ycu.jk16.mall.domain.ProductPhoto;
import com.ycu.jk16.mall.service.ProductService;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

@Controller
@RequestMapping(value = "/product")
public class ProductController {

	@Autowired
	@Qualifier("productService")
	private ProductService productService;

	@RequestMapping(value = "/list")
	public void list_do(@RequestParam(value = "categoryId", defaultValue = "-1") int categoryId, String keyword,
			@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
			@RequestParam(value = "orderBy", defaultValue = "") String orderBy, HttpServletResponse response) {
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		List<Object> list = new ArrayList<Object>();
		Map<String, Object> tempInfo;
		List<Product> products;

		try {
			params.put("categoryId", categoryId);
			params.put("keyword", keyword);
			params.put("pageNum", pageNum);
			params.put("pageSize", pageSize);
			params.put("orderBy", orderBy);
			products = productService.productList(params);
			int totalPages = productService.countProduct() / pageSize + productService.countProduct() % pageSize == 0
					? 0 : 1;
			for (int i = 0; i < products.size(); i++) {
				tempInfo = new HashMap<String, Object>();
				tempInfo.put("id", products.get(i).getId());
				tempInfo.put("categoryId", products.get(i).getCategory().getId());
				tempInfo.put("name", products.get(i).getName());
				tempInfo.put("subtitle", products.get(i).getSubtitle());
				tempInfo.put("mainImage", products.get(i).getMainImage());
				tempInfo.put("status", products.get(i).getStatus());
				tempInfo.put("price", products.get(i).getPrice());
				list.add(tempInfo);
			}

			tempInfo = new HashMap<String, Object>();
			tempInfo.put("pageNum", pageNum);
			tempInfo.put("pageSize", pageSize);
			tempInfo.put("size", products.size());
			tempInfo.put("orderBy", null);
			tempInfo.put("total", "");
			tempInfo.put("pages", "");
			tempInfo.put("list", list);
			tempInfo.put("firstPage", 1);
			tempInfo.put("prePage", pageNum > 1 ? pageNum - 1 : pageNum);
			tempInfo.put("nextPage", pageNum < totalPages ? pageNum + 1 : pageNum);
			tempInfo.put("lastPage", totalPages);
			tempInfo.put("isFisrtPage", pageNum == 1 ? "true" : "false");
			tempInfo.put("isLastPage", pageNum == totalPages ? "true" : "false");
			tempInfo.put("hasPreviousPage", pageNum > 1 ? "true" : "false");
			tempInfo.put("hasNextPage", pageNum < totalPages ? "true" : "false");

			// 加入json数据
			data.put("status", 0);
			data.put("data", tempInfo);
		} catch (Exception e) {
			e.printStackTrace();
			data.put("status", 10);
			data.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	@RequestMapping(value = "/detail")
	public void search_do(Integer productId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> pinfo = new HashMap<String, Object>();

		Product product;

		try {
			product = productService.selectById(productId);

			pinfo.put("id", product.getId());
			pinfo.put("categoryId", product.getCategory().getId());
			pinfo.put("parentCategoryId", product.getCategory().getParentCategory().getId());
			pinfo.put("name", product.getName());
			pinfo.put("subtitle", product.getSubtitle());
			pinfo.put("imageHost", product.getImageHost());
			pinfo.put("mainImage", product.getMainImage());
			List<ProductPhoto> photos = productService.selectProductPhotoListById(productId);
			String subImage = "[";
			if (photos.size() > 0) {
				subImage += "\"" + photos.get(0).getSubImage() + "\"";
			}
			for (int i = 1; i < photos.size(); i++) {
				subImage += ",\"" + photos.get(i).getSubImage() + "\"";
			}
			subImage += "]";
			pinfo.put("subImages", subImage);
			pinfo.put("detail", product.getDetail());
			pinfo.put("price", product.getPrice());
			pinfo.put("stock", product.getStock());
			pinfo.put("status", product.getStatus());
			pinfo.put("createTime", product.getCreateTime());
			pinfo.put("updateTime", product.getUpdateTime());

			// 加入json数据
			data.put("status", 0);
			data.put("data", pinfo);
		} catch (Exception e) {
			data.put("status", 1);
			data.put("msg", "没有权限");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

}
